package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.RoleMenuMapper;
import com.dk.mapper.TRoleMapper;
import com.dk.pojo.RoleMenuExample;
import com.dk.pojo.TRole;
import com.dk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service //spring实例化对象的注解
public class RoleServiceImpl implements RoleService {
    @Autowired
    private TRoleMapper tRoleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    //查询
    @Override
    public List<TRole> getInfo() {
        return tRoleMapper.selectByExample(null);
    }
    //新增

    @Override
    public boolean addInfo(TRole r, Integer[] mids) {
        try {
            //增加角色表  返回主键
            Integer i = tRoleMapper.insertInfoReturnId(r);
            //角色主键id
            Integer rid = r.getRoleId();
            //增加中间表
            Integer i2 = roleMenuMapper.insertBatch(rid,mids);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    //修改回显
    @Override
    public TRole toUpate(Integer rid) {
        return tRoleMapper.selectByPrimaryKey(rid);
    }

    @Override
    public boolean updateInfo(TRole r, Integer[] mids) {
        //修改角色信息
        Integer i = tRoleMapper.updateByPrimaryKeySelective(r);
        //删除
        RoleMenuExample re = new RoleMenuExample();
        RoleMenuExample.Criteria criteria = re.createCriteria();
        criteria.andIdEqualTo(r.getRoleId());
        roleMenuMapper.deleteByExample(re);
        //增加中间表
        Integer i2 = roleMenuMapper.insertBatch(r.getRoleId(),mids);
        return true;
    }

    //删除
    @Override
    public boolean deleteInfo(Integer rid) {
        try {
            int i = tRoleMapper.deleteByPrimaryKey(rid);
            // 删除
            RoleMenuExample re = new RoleMenuExample();
            RoleMenuExample.Criteria criteria = re.createCriteria();
            criteria.andIdEqualTo(rid);
            roleMenuMapper.deleteByExample(re);
            return true;
        }catch (Exception e){
            throw  e;
        }
    }
}
